package com.i1510.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.i1510.mapper.mti.HsyMapper;
import com.i1510.po.OrderItemPo;
import com.i1510.po.OrderPo;
import com.i1510.service.IHsyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class HsyServiceImpl implements IHsyService {

    @Autowired
    private HsyMapper hsyMapper;

    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 更新上传到hsy的订单
     * @param
     * @return
     */
    @Override
    public boolean updateOrderPo4Jst(Map params) {
        return hsyMapper.updateOrderPo4Jst(params);
    }
    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 获取未上传的Jst订单表
     * @param
     * @return
     */
    @Override
    public List<Map> getSendJstOrderList(Map params) {
        List<Map> list = hsyMapper.getSendJstOrderList(params);
        return list;
    }

    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 查询数据库已存在的hsy订单
     * @param
     * @return
     */
    @Override
    public List<Map> getOrderList(Map params) {
        List<Map> list = hsyMapper.getOrderList(params);
        return list;
    }

    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 查询数据库已存在的hsy订单
     * @param
     * @return
     */
    @Override
    public List<Map> getOrderDetailList(Map params) {
        List<Map> list = hsyMapper.getOrderDetailList(params);
        return list;
    }
    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 通用解析订单
     * @param
     * @return
     */
    @Override
    public boolean parseOrderList(JSONObject jsonObject) {
        JSONObject jo = jsonObject.getJSONObject("data");

        JSONArray jsonArray = jo.getJSONArray("resultData");

        //订单抬头
        List<OrderPo> list = jsonArray.toJavaList(OrderPo.class);
        if(jsonArray.size()>0){

            list.parallelStream().map(orderPo -> {
                orderPo.setInsertTime(DateUtil.now());
                orderPo.setOrderDate(
                        DateUtil.format(DateUtil.parse(orderPo.getOrderDate()),
                                "yyyy-MM-dd HH:mm:ss"));

                orderPo.setShippingDate(
                        DateUtil.format(DateUtil.parse(orderPo.getShippingDate()),
                                "yyyy-MM-dd HH:mm:ss"));

                orderPo.setFinishDate(
                        DateUtil.format(DateUtil.parse(orderPo.getFinishDate()),
                                "yyyy-MM-dd HH:mm:ss"));

                orderPo.setPayDate(
                        DateUtil.format(DateUtil.parse(orderPo.getPayDate()),
                                "yyyy-MM-dd HH:mm:ss"));

                return orderPo;
            }).collect(Collectors.toList());

            hsyMapper.updateOrder(list);
            //订单明细
            for(int i=0;i<jsonArray.size();i++){
                JSONObject job = jsonArray.getJSONObject(i);
                String orderId = job.getString("orderId");
                List<OrderItemPo> itemList = job.getJSONArray("itemlist").toJavaList(OrderItemPo.class);
                itemList.parallelStream().map(orderItemPo -> {
                    orderItemPo.setOrderId(orderId);
                    orderItemPo.setInsertTime(DateUtil.now());
                    return orderItemPo;
                }).collect(Collectors.toList());
                hsyMapper.updateOrderItem(itemList);
            }
        }



        return true;
    }

     /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 解析订单-付款订单
     * @param
     * @return
     */
     @Override
     @Deprecated
     public boolean getOrderListByPay(JSONObject jsonObject) {
        JSONObject jo = jsonObject.getJSONObject("data");

        JSONArray jsonArray = jo.getJSONArray("resultData");

        //订单抬头
        List<OrderPo> list = jsonArray.toJavaList(OrderPo.class);
        if(jsonArray.size()>0){

            list.parallelStream().map(orderPo -> {
                orderPo.setInsertTime(DateUtil.now());
                return orderPo;
            }).collect(Collectors.toList());

            hsyMapper.updateOrder(list);
            //订单明细
            for(int i=0;i<jsonArray.size();i++){
                JSONObject job = jsonArray.getJSONObject(i);
                String orderId = job.getString("orderId");
                List<OrderItemPo> itemList = job.getJSONArray("itemlist").toJavaList(OrderItemPo.class);
                itemList.parallelStream().map(orderItemPo -> {
                    orderItemPo.setOrderId(orderId);
                    orderItemPo.setInsertTime(DateUtil.now());
                    return orderItemPo;
                }).collect(Collectors.toList());
                hsyMapper.updateOrderItem(itemList);
            }
        }



        return true;
    }
    /**
     * @author rambo
     * @date 2022-11-04 15:29
     * @desc 解析订单-最后修改的订单
     * @param
     * @return
     */
    @Override
    @Deprecated
    public boolean getOrderListByChange(JSONObject jsonObject) {
        JSONObject jo = jsonObject.getJSONObject("data");

        JSONArray jsonArray = jo.getJSONArray("resultData");

        //订单抬头
        List<OrderPo> list = jsonArray.toJavaList(OrderPo.class);
        if(jsonArray.size()>0){

            list.parallelStream().map(orderPo -> {
                orderPo.setInsertTime(DateUtil.now());
                return orderPo;
            }).collect(Collectors.toList());

            hsyMapper.updateOrder(list);
            //订单明细
            for(int i=0;i<jsonArray.size();i++){
                JSONObject job = jsonArray.getJSONObject(i);
                String orderId = job.getString("orderId");
                List<OrderItemPo> itemList = job.getJSONArray("itemlist").toJavaList(OrderItemPo.class);
                itemList.parallelStream().map(orderItemPo -> {
                    orderItemPo.setOrderId(orderId);
                    orderItemPo.setInsertTime(DateUtil.now());
                    return orderItemPo;
                }).collect(Collectors.toList());
                hsyMapper.updateOrderItem(itemList);
            }
        }



        return true;
    }


}
